{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "9f53b4c2-7fb4-4f47-90f4-f3572a393db0",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_openai import ChatOpenAI\n",
    "from langchain.output_parsers import CommaSeparatedListOutputParser\n",
    "from langchain.prompts import ChatPromptTemplate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d594f4d0-0363-4368-b240-5fbf804e6ea1",
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt = ChatPromptTemplate.from_messages([\n",
    "    (\"system\", \"{parser_instructions}\"),\n",
    "    (\"human\", \"列出5个{subject}色系的十六进制颜色码。\")\n",
    "])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "4363d310-31e8-4b1f-8e1e-9a30720c9d61",
   "metadata": {},
   "outputs": [],
   "source": [
    "output_parser = CommaSeparatedListOutputParser()\n",
    "parser_instructions = output_parser.get_format_instructions()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "784a4492-8c8b-40d1-b16c-af32c3699246",
   "metadata": {},
   "outputs": [],
   "source": [
    "model = ChatOpenAI(model=\"gpt-3.5-turbo\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "1afb3515-7a85-41b0-8983-6129adfb2919",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['#B57EDC', '#B55EDC', '#B53EDC', '#B51EDC', '#B50EDC']"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result = output_parser.invoke(model.invoke(prompt.invoke({\"subject\": \"莫兰迪\", \"parser_instructions\": parser_instructions})))\n",
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "faeb9650-d1d4-4bf1-b801-09c4d6f6f3e0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['#b392ac', '#bb84b8', '#c97dbf', '#d174c7', '#db6dd0']"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chat_model_chain = prompt | model | output_parser\n",
    "result = chat_model_chain.invoke({\"subject\": \"莫兰迪\", \"parser_instructions\": parser_instructions})\n",
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e1a14a08-cef7-4c50-b534-e54733f27111",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
